function setpaytype(type){
    console.log("支付类型："+type);
    if(type==1){
        $("#wechatpay").addClass("paytype-body-item-btn-selected");
        $("#payType").val(1);
        $("#balancepay").removeClass("paytype-body-item-btn-selected")
        $("#balancepay").addClass("paytype-body-item-btn");
    }else if(type==2){
        $("#balancepay").addClass("paytype-body-item-btn-selected");
        $("#payType").val(2);
        $("#wechatpay").removeClass("paytype-body-item-btn-selected")
        $("#wechatpay").addClass("paytype-body-item-btn");
    }
};
$(function(){
    server.getUserInfo(function (res) {
        console.log('userin fo ..')
        console.log(res)
        $("#mynickname").text(res.nickname);
        if(res.phone)
        $("#myphone").text(res.phone);
    })
   var orderinfo= JSON.parse( server.getCache("orderInfo")) ; 
    server.setCache("orderInfo",null);
    if(orderinfo.teacherId==-5){
        var goodinfo={};
        goodinfo.headimgurl="img/vip.jpg";
        goodinfo.nickname="开通VIP会员";
        goodinfo.skills="开通VIP会员"
        $("#headimg").attr("src","img/vip.jpg");
        server.loadData("#content_submit",goodinfo);
    }else if(orderinfo.teacherId==-2){
        var goodinfo={};
        goodinfo.headimgurl="img/shance.jpg";
        goodinfo.nickname="闪测订单";
        goodinfo.skills="闪测项目"
        $("#headimg").attr("src","img/shance.jpg");
        server.loadData("#content_submit",goodinfo);
    }else if(orderinfo.teacherId==-4){
        var goodinfo={};
        goodinfo.headimgurl="img/chongzhi.jpg";
        goodinfo.nickname="充值订单";
        goodinfo.skills="充值订单"
        $("#headimg").attr("src","img/chongzhi.jpg");
        // console.log("-------------")
        // console.log(goodinfo)
        orderinfo.goodinfo = goodinfo;
        // server.loadData("#content_submit",goodinfo);
    }
    else{
    server.req("wechat/teacher/info/"+orderinfo.teacherId,{},function (e){
        if(e.code==0){
            if(e.data){
                orderinfo.itemlist[0].images = e.data.headimgurl;
                if(e.data.headimgurl)
                    $("#headimg").attr("src",e.data.headimgurl);
            }

        }else{
            pop_up(e.msg);
        }
    });
    }
    server.loadData("#content_submit",orderinfo);

    //获取当前用户的余额信息
    server.req('wechat/memberwallet/getbymemberid',{},function (res) {
        if(res.code==0){
            console.log(res)
            $("#balance").text("账户余额：¥"+res.data.balance);
        }
    })

})

function submitorder_paybypaytype() {
    var formdata = $("#submitorder_frm_main").serializeObject();
    console.log(formdata)
    var type=formdata.payType;
    if(type==1){
        submitorder_submitpay();

    }else if(type==2){
        // pop_up('当前还不支持余额支付');
        // return;
        //余额支付
        // console.log("余额支付")
        var orderno= $("#content_submit").find("input[data-name=orderNo]");
        // console.log(orderno.val());
        paybybalance(orderno.val());

    }else{
        pop_up('支付方式错误')
    }
}
//余额支付
function paybybalance(orderId) {
    server.req("wechat/order/balancepay",{orderNo:orderId},function(data){
        console.log("余额支付");
        console.log(data);

        if(data.code!=0) {
            pop_up(data.msg);
            return ;
        }else{
            pop_up("支付成功");
            if(data.data.serviceType!=5){
                server.loadindex('page/order/orderlist/orderlist');
            }else{
                server.removeUser();
                server.loadindex('page/user/user');
            }

        }
    },null,"POST","application/x-www-form-urlencoded")
}
function submitorder_submitpay() {
    console.log("yyy");
   var e= $("#content_submit").find("input[data-name=orderNo]");
   // console.log(e.val())
    function onBridgeReady(res) {
        console.log(res)
        //var type = $('input:radio[name="paychoose"]:checked').val();
        WeixinJSBridge.invoke('getBrandWCPayRequest', res.data, function(data) {
            console.log(data)
            if (data.err_msg == "get_brand_wcpay_request:ok") {
                server.loadindex( "pay_success","redirect=page/order/orderlist/orderlist");
            } else {
                server.req("wechat/order/removePay?orderNo="+e.val());
                server.setCache("errorMsg","下单失败！");
                server.loadindex( "error","redirect=page/order/orderlist/orderlist");
            } // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
        });
    }
    server.req("wechat/order/pay",{orderNo:e.val()},function(data){

        if(data.code!=0) {
            //	 server.req("wechat/order/removePay");
            //server.setCache("errorMsg","下单失败！");
            //	server.loadindex( "error","redirect=qbOrder");
            pop_up(data.msg);
            return ;
        }

        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', 	onBridgeReady);
            }
        } else {
            onBridgeReady(data);
        }

    },()=>{},"POST","application/x-www-form-urlencoded")



}